﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static CookieContainer cc = new CookieContainer();
        public static string hash;
        public static string cik;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //textBox1.Text = PostModel("http://www.xksoft.com/csharp/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&inajax=0", "formhash=15695c0&referer=&loginfield=username&username=易水寒&password=fanmin&questionid=0&answer=");

            //textBox1.Text = PostModel("http://www.meqzone.com/member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&floatlogin=yes&loginhash=L4MMO&inajax=1 HTTP/1.1", "formhash=e6cf39e9&referer=http%3A%2F%2Fwww.meqzone.com%2Fthread-172577-1-1.html&username=fanmin&password=fanmin&questionid=0&answer=");

            textBox1.Text = PostModel("http://www.80bc.cn/dede/login.php", "gotopage=%2Fdede%2Findex.php&dopost=login&adminstyle=newdedecms&userid=admin&pwd=@fanmin1991&sm1=%B5%C7%C2%BC");



        }

        public static string fabu(string strUrl, string strParm)
        {
            MessageBox.Show(cc.Count.ToString());
            Encoding mye = Encoding.GetEncoding("gbk");
            Encoding encode = mye;
            byte[] arrB = encode.GetBytes(strParm);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            myReq.CookieContainer = cc;
            myReq.Headers.Add("cookie",cik);
            myReq.Method = "POST";
           
            myReq.ContentType = "multipart/form-data; boundary=---------------------------7db196db0ad2";
            myReq.Referer = "http://www.80bc.cn/dede/article_add.php";
            myReq.ContentLength = arrB.Length;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            WebResponse myResp = null;

            //接收HTTP做出的响应  
            myResp = myReq.GetResponse();

          
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            readStream.Close();
            myResp.Close();
            return str;
        }
        public static string PostModel(string strUrl, string strParm)
        {

            Encoding encode = System.Text.Encoding.Default;

            byte[] arrB = encode.GetBytes(strParm);


            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            if (cc.Count == 0)
            {
                myReq.CookieContainer = new CookieContainer();
                cc = myReq.CookieContainer;
            }
            else
            {
                myReq.CookieContainer = cc;
            }
            myReq.Method = "POST";
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.ContentLength = arrB.Length;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            HttpWebResponse myResp = null;
            try
            {
                //接收HTTP做出的响应  
                myResp = (HttpWebResponse)myReq.GetResponse();

            }
            catch (Exception e)
            {
                int ii = 0;
            }
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            xkCookies.upcookie(myResp.Cookies);
            myReq.CookieContainer = cc;
            foreach (Cookie cookie in myResp.Cookies)
            {
                cc.Add(cookie);
                cik = cik + cc.ToString();
            }


            readStream.Close();
            myResp.Close();
            hash = str.Substring(str.IndexOf("formhash=") + 9);
            hash = hash.Remove(hash.IndexOf("\""));
            return str;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = fabu("http://www.80bc.cn/dede/article_add.php",File.ReadAllText("data.txt"));

        }
    }

}
